﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace HOOMT
{
    class PrimitiveObjectDiagram : ObjectDiagram
    {
        //这些属性都不要保存到数据库
        
        

        public PrimitiveObjectDiagram(Point point, int width = 140, int height = 90)
            : base(point, width, height)
        {
            name = "PrimitiveObject";
        }
        public PrimitiveObjectDiagram(PrimitiveObjectDiagram diagram)
            : base(new Point(), diagram.width, diagram.height)
        {
            this.id = diagram.id;
            this.AttributeList = diagram.AttributeList;
            this.MethodList = diagram.MethodList;
        }
        //绘图函数
        //************************************************************************************
        /// <summary>
        /// 绘制图形对象本身
        /// 绘制图像对象多包含的文字
        /// 图形对象的大小随着文字量而改变
        /// </summary>
        /// <param name="g"></param>
        /// <param name="m"></param>
        public override void Drawing(Graphics g, OriginalModelForm m)
        {
            //文字显示开始位置距左上角（10，5）单位
            Point pt = new Point(upLeftpoint.X + 10, upLeftpoint.Y + fontUpLineSpace);

            pt = drawingAttributes(g, pt);

            g.DrawLine(pen, upLeftpoint.X, pt.Y, upLeftpoint.X + width, pt.Y);
            pt.Y += fontUpLineSpace;

            pt = drawingMethods(g, pt);

            g.DrawLine(pen, upLeftpoint.X, pt.Y, upLeftpoint.X + width, pt.Y);
            pt.Y += fontUpLineSpace;

            pt = drawingNFR(g, pt);

            if (pt.Y > upLeftpoint.Y + height)
                height = pt.Y - upLeftpoint.Y;
            Rectangle rect = new Rectangle(upLeftpoint.X, upLeftpoint.Y, width, height);
            g.DrawRectangle(pen, rect);
            
            base.Drawing(g, m);  
        }
        
        public override int getMinHeight()
        {
            return (MethodList.Count()*2 + AttributeList.Count()*2 + ObjectNFRList.Count()) * 9 + 24;//每天记录占用10个单位的高度，记录与直线的间隔总共21个单位高度
        }

        protected override string cutShortString(string str)
        {
            return base.cutShortString(str);
        }

        private Point drawingAttributes(Graphics g, Point pt)
        {
            foreach (Attribute attr in AttributeList)
            {
                g.DrawString(cutShortString(attr.getShowString()), drawFont, drawBrush, pt);
                pt.Y += fontHeight;
            }
            if (pt.Y < upLeftpoint.Y + height / 3)
                pt.Y = upLeftpoint.Y + height / 3;
            pt.Y += fontDownLineSpace;
            return pt;
            
        }

        private Point drawingMethods(Graphics g, Point pt)
        {
            foreach (Method method in MethodList)
            {
                g.DrawString(cutShortString(method.getShowString()), drawFont, drawBrush, pt);
                pt.Y += fontHeight;
            }
            if (pt.Y < upLeftpoint.Y + 2 * height / 3)
                pt.Y = upLeftpoint.Y + 2 * height / 3;
            pt.Y += fontDownLineSpace;
            return pt;
        }

        private Point drawingNFR(Graphics g, Point pt)
        {
            if (ObjectNFRList.Count != 0)
            {
                g.DrawString(cutShortString("O_" + name + "=[" + getNFRListString(ObjectNFRList) + "]"), drawFont, drawBrush, pt);
                pt.Y += fontHeight;
            }
            foreach (Attribute attr in AttributeList)
            {
                if (attr.AttributeNFRList.Count != 0)
                {
                    g.DrawString(cutShortString("A_" + attr.AttributeName + "=[" + getNFRListString(attr.AttributeNFRList) + "]"), drawFont, drawBrush, pt);
                    pt.Y += fontHeight;
                }
            }
            foreach (Method method in MethodList)
            {
                if (method.MethodNFRList.Count != 0)
                {
                    g.DrawString(cutShortString("M_" + method.MethodName + "=[" + getNFRListString(method.MethodNFRList) + "]"), drawFont, drawBrush, pt);
                    pt.Y += fontHeight;
                }
            }
            pt.Y += fontDownLineSpace;
            return pt;
        }

    }
}
